<?php
function writeFile($file, $str, $mode = 'w') {
	$oldmask = @umask ( 0 );
	$fp = @fopen ( $file, $mode );
	@flock ( $fp, 3 );
	if (! $fp)
		Return false;
	else {
		@fwrite ( $fp, $str );
		@fclose ( $fp );
		@umask ( $oldmask );
		Return true;
	}
}
function getIpAddress() {
	$ip = "";
	if (isset ( $_SERVER ["HTTP_X_FORWARDED_FOR"] )) {
		if (isset ( $_SERVER ["HTTP_CLIENT_IP"] )) {
			$proxy = $_SERVER ["HTTP_CLIENT_IP"];
		} else {
			$proxy = $_SERVER ["REMOTE_ADDR"];
		}
		$ip = $_SERVER ["HTTP_X_FORWARDED_FOR"];
	} else {
		if (isset ( $_SERVER ["HTTP_CLIENT_IP"] )) {
			$ip = $_SERVER ["HTTP_CLIENT_IP"];
		} else {
			$ip = $_SERVER ["REMOTE_ADDR"];
		}
	}
	return $ip;
}
function file_ct($ct) {
	if ($ct == "gif")
		return "image/gif";
	if ($ct == "jpg")
		return "image/pjpeg";
	if ($ct == "png")
		return "image/png";
	if ($ct == "bmp")
		return "image/bmp";
	return "application/octet-stream";
}
function encodeID($id) {
	$code = array_merge ( range ( '0', '9' ), range ( 'a', 'z' ), range ( 'A', 'Z' ) );
	$ret = '';
	do {
		$ret = $code [$id % 62] . $ret;
		$id = ( int ) ($id / 62);
	} while ( $id );
	return $ret;
}
function decodeID($str) {
	$code = array_flip ( array_merge ( range ( '0', '9' ), range ( 'a', 'z' ), range ( 'A', 'Z' ) ) );
	$ret = 0;
	$len = strlen ( $str );
	for($i = 0; $i != $len; ++ $i) {
		$ret *= 62;
		$ret += $code [$str [$i]];
	}
	return $ret;
}
function showErrorImage($code) {
	/* Disable Caching */
	header ( "Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
	header ( "Last-Modified: " . gmdate ( "D, d M Y H:i:s" ) . " GMT" );
	header ( "Cache-Control: no-cache, must-revalidate" );
	header ( "Pramga: no-cache" );
	header ( "Content-type: image/pjpeg" );
	$filename = 'static/500.jpg';
	if ($code == 404)
		$filename = 'static/404.jpg';
	else if ($code == 503)
		$filename = 'static/503.jpg';
	$file = fopen ( $filename, "r" );
	echo fread ( $file, filesize ( $filename ) );
	fclose ( $file );
}
function getCode($length = 32, $mode = 0) {
	switch ($mode) {
		case '1' :
			$str = '1234567890';
			break;
		case '2' :
			$str = 'abcdefghijklmnopqrstuvwxyz';
			break;
		case '3' :
			$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case '4' :
			$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
			break;
		case '5' :
			$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
			break;
		case '6' :
			$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
			break;
		default :
			$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
			break;
	}
	
	$result = '';
	$l = strlen ( $str );
	
	for($i = 0; $i < $length; $i ++) {
		$num = rand ( 0, $l );
		$result .= $str [$num];
	}
	return $result;
}

?>